<?php
/**
 * Created by JetBrains PhpStorm.
 * User: q
 * Date: 13-12-22
 * Time: 上午12:30
 * To change this template use File | Settings | File Templates.
 */
class Dimensions_code_lib {
    public function get_access_token($appid,$secret){
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
        $json=self::http_request_json($url);//这个地方不能用file_get_contents
        $data=json_decode($json,true);
        if($data['access_token']){
            return $data['access_token'];
        }else{
            return "获取access_token错误";
        }
    }
    function http_request_json($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    function http_pic_json($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    function http_post_json($url,$data_string){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    function post($token,$id){
//        $token = 'jrLq86DTRfSYeYDU-t63lqt6WDFYC6xDULPA4trYSEi_NCmhJnQ0VEP_JjljHTcx0Lw1Sp-3nxB1iRoJDk60dKURzY3Hu846PNvpInr5LCrc2Xtl7qSkKCbz0U0KUdK-Qucj9gTV6ya0TWVNPfxn6Q';
        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$token;
//        $id = '234234';
        $data = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$id.'}}}';
        $json=self::http_post_json($url,$data);//这个地方不能用file_get_contents
        $data=json_decode($json,true);
        if($data['ticket']){
            return urlencode( $data['ticket']);
        }else{
            return "error";
        }
    }

    function set_menu($token,$data){
//        $token = 'jrLq86DTRfSYeYDU-t63lqt6WDFYC6xDULPA4trYSEi_NCmhJnQ0VEP_JjljHTcx0Lw1Sp-3nxB1iRoJDk60dKURzY3Hu846PNvpInr5LCrc2Xtl7qSkKCbz0U0KUdK-Qucj9gTV6ya0TWVNPfxn6Q';
        $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$token;
//        $id = '234234';
//        $data = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$id.'}}}';
        $json=self::http_post_json($url,$data);//这个地方不能用file_get_contents
        $data=json_decode($json,true);

        return $data['errmsg'];
    }

    function get_pic($token,$id,$type){
        $ticket = $this->post($token,$id);
        if($ticket == 'error'){
            return 'error';
        }else{
            if($type == 1){
                $url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticket;
                $back = $url;
//        echo $url;
//        $src = $this->http_pic_json($url);
            }elseif($type == 2){
                $back = $ticket;
            }
            return $back;
        }
    }
}